<?php
namespace app\push\controller;

// use think\worker\Server;
use GatewayWorker\Gateway;
use think\Request;
class Worker extends Server
{
    
	protected $_deviceStatus;

	protected $key = 'pCKvp5hRNm7HGbcucBjCuwmYSZ53UyM8';
    protected $maxSendBufferSize=10485760;
	
    protected $socket = 'tcp://0.0.0.0:8702';

    private function checkKey($data){
        return true;
    	$key = substr($data,0,32);
        // set_log('接收到的key：'.$key);
    	$end = substr($data,32);
        // set_log('截取的字符串：'.$end);
        // set_log('加密的字符串：'.$end .$this->key);
    	$com = md5($end .$this->key);
        // set_log('加密得到的key：'.$com);
    	if($key == $com){
    		return true;
    	}
    }

    /**
     * 收到信息
     * @param $connection
     * @param $data
     */
    public function onMessage($connection, $message)
    {
        dump($connection->id);
        dump($connection->getLocalAddress());

        dump($message);
        //set_log($connection);
        set_log($message);
        $data = substr($message,1,-1);
    	/*$bool = $this->checkKey($data);
        //dump($bool);
    	if($bool){//验证签名是否正确
            
    		$data = substr($data,32);

	    	$totle = explode(',', $data);
            $info = explode('*', $totle[0]);
            //dump($info);
	    	if(isset($info[3]) && $info[3] == 'KA'){

	    		//如果是链路保持，原样返回数据;
    			$this->_deviceStatus = new \app\common\model\DeviceStatus();
    			$this->_deviceStatus->addstatus($info);
    			$connection->send('['.$totle[0].']');
	        	
		    }else if(isset($info[3]) && $info[3] == 'UD'){
		    	
	    		$this->_deviceStatus = new \app\common\model\DeviceMessage();
    			$this->_deviceStatus->addmessage($info['1'],$totle);
		    }else if(isset($info[0]) && $info[0] == 'SG'){*/

                Gateway::$registerAddress = '127.0.0.1:8702';
                $ip = $connection->getRemoteIp();
                // dump($ip);
                dump('call me!');
                // 建立本地80端口的异步连接
                $connection_to_80 = new AsyncTcpConnection('tcp://172.18.28.38:8702');
                // 设置将当前客户端连接的数据导向80端口的连接
                $connection->pipe($connection_to_80);
                // 设置80端口连接返回的数据导向客户端连接
                $connection_to_80->pipe($connection);
                // 执行异步连接
                $connection_to_80->connect();
                // dump($message);
                // $connection->send('hello',1);
                // $bool = $connection->pipe($)
                $bool = $connection_to_80->send($message);
                dump($bool);
            /*}

    	}   */ 	
    	
    }

    /**
     * 当连接建立时触发的回调函数
     * @param $connection
     */
    public function onConnect($connection)
    {
        $connection->send('ok');
    }

    /**
     * 当连接断开时触发的回调函数
     * @param $connection
     */
    public function onClose($connection)
    {
        
    }

    /**
     * 当客户端的连接上发生错误时触发
     * @param $connection
     * @param $code
     * @param $msg
     */
    public function onError($connection, $code, $msg)
    {
        echo "error $code $msg\n";
    }

    /**
     * 每个进程启动
     * @param $worker
     */
    public function onWorkerStart($worker)
    {

    }
}